﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.Interfaces;
using System.Data;
using AMS.DataAccess.Trademark.BO;

public partial class RecvFile_AddRecvFile : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ParseIntParameter("type", out fileType))
        {
            fileType = 1;
        }

        LoadDetailCtrl(fileType);
        if (!IsPostBack)
        {
            BindRFCategory(fileType);
            drpRFType.SelectedValue = fileType.ToString();

            drpRFType.Attributes["onchange"] = "javascript:onRFTypeSelectChanged(this);";
            lbRF.Attributes["onchange"] = "javascript:onRFSelected(this);";
        }
    }

    #region Properties and Fields
    int fileType = 1;
    static string[] SubControlPaths = new string[] {
        "",
        "~/RecvFile/ucRecvFileDetail.ascx",
        "~/RecvFile/ucInstructionRecvFileDetail.ascx",
        "~/RecvFile/ucRecvFileDetail.ascx"
    };

    static string[] RFCategoryFilters = new string[] {
        "",
        "Source = 'O'",
        "Source = 'C' AND Type = 'Y'",
        "Source = 'C' AND Type <> 'Y'"
    };

    /// <summary>
    /// 标识是否新申请指示函
    /// </summary>
    bool IsInstructionFile
    {
        get { return fileType == 2; }
    }

    void LoadDetailCtrl(int value)
    {
        string path = SubControlPaths[value];
        Control ctrl = LoadControl(path);
        phContainer.Controls.Add(ctrl);
    }

    IRecvFileEditorCtrl _editCtrl;
    /// <summary>
    /// The recv file editor control
    /// </summary>
    IRecvFileEditorCtrl EditCtrl
    {
        get
        {
            if (_editCtrl == null && phContainer.Controls.Count > 0)
            {
                _editCtrl = phContainer.Controls[0] as IRecvFileEditorCtrl;
            }
            return _editCtrl;
        }
    }
    #endregion

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (EditCtrl != null)
        {
            RecvFileInfo rf = new RecvFileInfo();
            if (!EditCtrl.GetRecvFileInfo(rf))
            {
                MsgInfo(EditCtrl.ErrorMessage);
            }
            else
            {
                // give default value
                IRecvFileManager mgr = ManagerFactory.CreateRecvFileManager();
                mgr.AddRecvFile(CallContext, rf);
                MsgInfo("已添加收文：" + rf.RFName);
                // 如果是新申请收文，则直接生成立卷任务
                if (IsInstructionFile)
                {
                    RecvFile_ucInstructionRecvFileDetail ctrl = (EditCtrl as RecvFile_ucInstructionRecvFileDetail);
                    TaskGenerator gen = new TaskGenerator(CallContext, rf);
                    int assignedUserID = 0;
                    if (!string.IsNullOrEmpty(ctrl.TaskAssignedUserID))
                    {
                        int.TryParse(ctrl.TaskAssignedUserID, out assignedUserID);
                    }
                    gen.GenerateTask(assignedUserID);
                }
            }
        }
    }

    void BindRFCategory(int value)
    {
        DataTable dt = RFCategoryProvider.GetRFCategory(Application);
        dt.DefaultView.RowFilter = RFCategoryFilters[value];

        lbRF.DataSource = dt.DefaultView;
        lbRF.DataTextField = "RFName";
        lbRF.DataValueField = "RFIndex";
        lbRF.DataBind();
    }
}
